package com.wuhuahe.luanfeng.javabio;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * Created By wuhuahe
 * author:游龙
 * Date: 2018-07-09
 * Time: 20:51
 * Desc:
 */
public class ThreadPoolTimeServer {

    public static void main(String[] args) {
        int port = 8080;
        if(args != null && args.length > 0){
            try{
                port = Integer.valueOf(args[0]);
            }catch (NumberFormatException e){}
        }

        ServerSocket server = null;
        try{
            server = new ServerSocket(port);
            System.out.println("The time server starts in port:" + port);
            Socket socket = null;
            TimeServerHandlerExecutePool singleExecutor = new TimeServerHandlerExecutePool(50, 10000);
            while (true){
                socket = server.accept();
                singleExecutor.execute(new TimeServerHandler(socket));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(server != null){
                try {
                    System.out.println("Time server close");
                    server.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
